#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <time.h>
#pragma comment(lib,"Winmm.lib")

#define Width 950         //画面
#define High 640

int SMn;              //一号水母数目
int HBn;   

int  SM_Fx,SM_Fy;         //汉堡包数目

int HMBBx,HMBBy;          //海绵宝宝位置坐标
int SMx[3],SMy[3];        //第一类水母位置坐标
int HBx[4],HBy[4];
int direction[3];            //水母移动方向
#define zk_number_x 21    //迷宫的行数
#define zk_number_y 31    //迷宫的列数

IMAGE img_zk,img_HMBB1,img_HMBB2,img_HB1,img_HB2;

int map[zk_number_y+2][zk_number_x+2];
void create(int x,int y)  //随机生成迷宫
{
	int c[4][2]={0,1,1,0,0,-1,-1,0}; //四个方向
    int i,j,t;
    //将方向打乱
    for(i=0;i<4;i++)
	{
		j=rand()%4;
		t=c[i][0];
		c[i][0]=c[j][0];
		c[j][0]=t;
		t=c[i][1];
		c[i][1]=c[j][1];
		c[j][1]=t;
	}
	map[x][y]=2;
	for(i=0;i<4;i++)
	{
		if(map[x+2*c[i][0]][y+2*c[i][1]]==1)
		{
			map[x+c[i][0]][y+c[i][1]]=2;
			create(x+2*c[i][0],y+2*c[i][1]);
		}
	}
}
//单人模式地图
void plant1()
{
	int i,j;
	srand((unsigned)time(NULL)); //初始化随即种子
	
	for(i=0;i<=zk_number_y+1;i++)
	{
		for(j=0;j<=zk_number_x+1;j++)
		{
			if(i==0||i==zk_number_y+1||j==0||j==zk_number_x+1) //初始化迷宫
				map[i][j]=2;
			else 
				map[i][j]=1;
		}
	}
	create(2*(rand()%(zk_number_y/2)+1),2*(rand()%(zk_number_x/2)+1)); //从随机一个点开始生成迷宫，该点行列都为偶数
	map[1][2]=3; //给定入口
	map[zk_number_y][zk_number_x-1]=3; //给定出口
	for(i=1;i<=zk_number_y;i++)
	{
		for(j=1;j<=zk_number_x;j++) //画出迷宫
		{
			if(map[i][j]==1)
                putimage(30*(i-1)+5,30*(j-1),&img_zk);
		}
		printf("\n");
	}
}

//生成随机移动的水母
void creatSM1()
{
	for(SMn=0;SMn<3;SMn++)
	{
	    int i,j;
	    i=rand()%29+2;
	    j=rand()%19+2;
	    while(map[i][j]==1||map[i][j]==3||map[i][j]==4||map[i][j]==5)//防止水母与墙壁、汉堡包、其他水母重合
		{
		    i=rand()%29+2;
	        j=rand()%19+2;
		}
		map[i][j]=4;    //标记生成水母位置
	    SMx[SMn]=30*i-25;
	    SMy[SMn]=30*j-30;
	}
}
//随机生成汉堡包
void creatHB()
{
	int HBn=4;
	for(HBn=0;HBn<4;HBn++)
	{
	    int i,j;
	    i=rand()%29+2;
	    j=rand()%19+2;
	    while(map[i][j]==1||map[i][j]==3||map[i][j]==4||map[i][j]==5)//防止汉堡包与墙壁、水母、其他汉堡包重合
		{
		    i=rand()%29+2;
	        j=rand()%19+2;
		}
		map[i][j]=5;    //标记生成汉堡包位置
	    HBx[HBn]=30*i-25;
	    HBy[HBn]=30*j-30;
	}
}

void startup()
{
    initgraph(Width,High);
	for(int i=0;i<3;i++)//随机生成行进方向
	{
		direction[i]=rand()%4+1;
	}
    
	HMBBx=5;
	HMBBy=30;
	
	SM_Fx=875;
	SM_Fy=570;
	loadimage(&img_zk,"E:\\HMBBzhuankuai.png");	//导入砖块
	loadimage(&img_HMBB1,"E:\\HMBBwalk1.png");//导入海绵宝宝
	loadimage(&img_HMBB2,"E:\\HMBBwalk2.png");
	loadimage(&img_HB1,"E:\\HMBB_hanbao1.png");//导入汉堡包
	loadimage(&img_HB2,"E:\\HMBB_hanbao2.png");
	plant1();
	creatSM1();
	creatHB();
    for(HBn=0;HBn<4;HBn++)
	{
	    putimage(HBx[HBn],HBy[HBn],&img_HB2,NOTSRCERASE);	
        putimage(HBx[HBn],HBy[HBn],&img_HB1,SRCINVERT);
	}
	BeginBatchDraw();
}

void show()
{
	
	putimage(HMBBx,HMBBy,&img_HMBB2,NOTSRCERASE);//	导入海绵宝宝
	putimage(HMBBx,HMBBy,&img_HMBB1,SRCINVERT);
	
	for(SMn=0;SMn<3;SMn++)
	{
	    putimage(SMx[SMn],SMy[SMn],&img_HMBB2,NOTSRCERASE);	
        putimage(SMx[SMn],SMy[SMn],&img_HMBB1,SRCINVERT);
	}
	putimage(SM_Fx,SM_Fy,&img_HMBB2,NOTSRCERASE);	
    putimage(SM_Fx,SM_Fy,&img_HMBB1,SRCINVERT);
	FlushBatchDraw();
	Sleep(50);

}

void updateWithoutInput()
{
	int i,j;
	int temp_direction[3];//记录行进方向
	for(SMn=0;SMn<3;SMn++)
	{
	    i=(SMx[SMn]+25)/30;  //水母位置对应行列数
	    j=(SMy[SMn]+30)/30;
        direction[SMn]=rand()%4+1;
		if(direction[SMn]==1&&(map[i-1][j]==2||map[i-1][j]==4))
		{
            clearrectangle(SMx[SMn],SMy[SMn],SMx[SMn]+30,SMy[SMn]+30);
			SMx[SMn]=SMx[SMn]-30;
		}	
		else if(direction[SMn]==2&&(map[i+1][j]==2||map[i+1][j]==4))
		{
            clearrectangle(SMx[SMn],SMy[SMn],SMx[SMn]+30,SMy[SMn]+30);
			SMx[SMn]=SMx[SMn]+30;
		}
		else if(direction[SMn]==3&&(map[i][j-1]==2||map[i][j-1]==4))
		{
			clearrectangle(SMx[SMn],SMy[SMn],SMx[SMn]+30,SMy[SMn]+30);
			SMy[SMn]=SMy[SMn]-30;
		}	
		else if(direction[SMn]==4&&(map[i][j+1]==2||map[i][j+1]==4))
		{
			clearrectangle(SMx[SMn],SMy[SMn],SMx[SMn]+30,SMy[SMn]+30);
		    SMy[SMn]=SMy[SMn]+30;
		}
		//当水母遇到汉堡，汉堡消失
		else if(direction[SMn]==1&&map[i-1][j]==5)
		{
            clearrectangle(SMx[SMn],SMy[SMn],SMx[SMn]+30,SMy[SMn]+30);
			clearrectangle(SMx[SMn]-30,SMy[SMn],SMx[SMn],SMy[SMn]+30);
			SMx[SMn]=SMx[SMn]-30;
		}	
		else if(direction[SMn]==2&&map[i+1][j]==5)
		{
            clearrectangle(SMx[SMn],SMy[SMn],SMx[SMn]+30,SMy[SMn]+30);
			clearrectangle(SMx[SMn],SMy[SMn]+30,SMx[SMn]+60,SMy[SMn]+30);
			SMx[SMn]=SMx[SMn]+30;
		}
		else if(direction[SMn]==3&&map[i][j-1]==5)
		{
			clearrectangle(SMx[SMn],SMy[SMn],SMx[SMn]+30,SMy[SMn]+30);
			clearrectangle(SMx[SMn],SMy[SMn]-30,SMx[SMn]+30,SMy[SMn]);
			SMy[SMn]=SMy[SMn]-30;
		}	
		else if(direction[SMn]==4&&map[i][j+1]==5)
		{
			clearrectangle(SMx[SMn],SMy[SMn],SMx[SMn]+30,SMy[SMn]+30);
			clearrectangle(SMx[SMn],SMy[SMn]+30,SMx[SMn]+30,SMy[SMn]+60);
		    SMy[SMn]=SMy[SMn]+30;
		}
		else
		{
			direction[SMn]=rand()%4+1;
		}
	}
		
	/*	int x=(SM_Fx-5)/30;
	int y=SM_Fy/30;

		if (HMBBx < SM_Fx&&((map[x][y+1]==2||map[x][y+1]==4||map[x][y+1]==5)||(map[x+1][y+1]==2&&(map[x][y+1]==1||map[x][y+1]==3||map[x][y+1]==4||map[x][y+1]==5)&&(SM_Fx-5)%30>0))&&SM_Fy%30==0)
		{
			clearrectangle(SM_Fx,SM_Fy,SM_Fx+30,SM_Fy+30);
			SM_Fx = SM_Fx - 6;
			Sleep(70);
		}
		if (HMBBy > SM_Fy&&map[x+1][y+2]==2&&(SM_Fx-5)%30==0)
		{				clearrectangle(SM_Fx,SM_Fy,SM_Fx+30,SM_Fy+30);

			SM_Fy = SM_Fy + 6;
			Sleep(70);
		}
		if (HMBBx > SM_Fx&&map[x+2][y+1]==2&&SM_Fy%30==0)
		{	clearrectangle(SM_Fx,SM_Fy,SM_Fx+30,SM_Fy+30);
			SM_Fx = SM_Fx + 6;
			Sleep(70);
		}
		if (HMBBy < SM_Fy&&(map[x+1][y]==2||(map[x+1][y+1]==2&&(map[x+1][y]==1||map[x][y+1]==4||map[x][y+1]==5)&&SM_Fy%30>0))&&(SM_Fx-5)%30==0)
		{	clearrectangle(SM_Fx,SM_Fy,SM_Fx+30,SM_Fy+30);
			SM_Fy = SM_Fy - 6;
			Sleep(70);
		}
		else
		{
			int direction=rand()%4+1;
			if(direction==1&&(map[x][y+1]==2||map[x][y+1]==4))
			{
            clearrectangle(SM_Fx,SM_Fy,SM_Fx+30,SM_Fy+30);
			SM_Fx=SM_Fx-30;
			}	
		    else if(direction==2&&(map[x+2][y+1]==2||map[x+2][y+1]==4))
			{
             clearrectangle(SM_Fx,SM_Fy,SM_Fx+30,SM_Fy+30);
			SM_Fx=SM_Fx+30;
			}
		    else if(direction==3&&(map[x+1][y]==2||map[x+1][y]==4))
			{
			 clearrectangle(SM_Fx,SM_Fy,SM_Fx+30,SM_Fy+30);
			 SM_Fy=SM_Fy-30;
			}	
		    else if(direction==4&&(map[x+1][y+2]==2||map[x+1][y+2]==4))
		{
			 clearrectangle(SM_Fx,SM_Fy,SM_Fx+30,SM_Fy+30);
		    SM_Fy=SM_Fy+30;
		}
		}
	*/

}

void updateWithInput()
{
	char input;
	//海绵宝宝位置对应行列数
	int x=(HMBBx-5)/30;
	int y=HMBBy/30;
	if (kbhit())
	{
		input=getch();
		//确保海绵宝宝在路上行走
		if(input=='a'&&(map[x][y+1]==2||(map[x+1][y+1]==2&&(map[x][y+1]==1||map[x][y+1]==3)&&(HMBBx-5)%30>0))&&HMBBy%30==0)
		{
			clearrectangle(HMBBx,HMBBy,HMBBx+30,HMBBy+30);	// 清空画面全部矩形区域
			HMBBx=HMBBx-6;
		}
        if(input=='d'&&map[x+2][y+1]==2&&HMBBy%30==0)
		{
			clearrectangle(HMBBx,HMBBy,HMBBx+30,HMBBy+30);
			HMBBx=HMBBx+6;
		}
		if(input=='w'&&(map[x+1][y]==2||(map[x+1][y+1]==2&&map[x+1][y]==1&&HMBBy%30>0))&&(HMBBx-5)%30==0)
		{
			clearrectangle(HMBBx,HMBBy,HMBBx+30,HMBBy+30);
			HMBBy=HMBBy-6;
		}
		if(input=='s'&&map[x+1][y+2]==2&&(HMBBx-5)%30==0)
		{
			clearrectangle(HMBBx,HMBBy,HMBBx+30,HMBBy+30);
			HMBBy=HMBBy+6;
		}
	}
}

void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}

void main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	gameover();

}

